package com.mla.fastdfs.filter;

import com.mla.fastdfs.commom.BaseException;
import com.mla.fastdfs.commom.Result;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authc.UnknownAccountException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
@Slf4j
public class MyExceptionHandler {

    @ExceptionHandler(value = Exception.class)
    public Result handler(Exception e) {
        if(e instanceof UnknownAccountException){
            return Result.error("未登录");
        }

        if (e instanceof BaseException) {
            BaseException baseException = (BaseException) e;
            if (baseException.getCode() != null) {
                return Result.build(baseException.getCode(), baseException.getMsg());
            }
            return Result.error(baseException.getMessage());
        }
        log.error(e.getMessage(),e);
        return Result.build("系统错误");
    }
}
